************Tables 3 and 4********************


*Set root data directory
local rootdir
cd "`rootdir'"




local round 43
use round`round'_edit, clear


merge 1:1 sector subround region subsample segment substratum fsu hhno using round`round'_hh, keepusing(mpce hhsize d* nic* mult)
drop hq* hx*
drop *cloth* *intox* *light*
drop _merge

gen state=floor(region/10)

******
gen statecode=state
cap: gen district=.
cap: gen stratum=.
cap: gen districtcode=district
cap: replace districtcode=stratum if round==50
sort sector region fsu
bysort fsu sector: egen minddist=min(districtcode)
bysort fsu sector: egen maxddist=max(districtcode)
replace districtcode=minddist if districtcode==.
replace districtcode=maxddist if districtcode==0 & maxddist~=.
merge m:m statecode districtcode using nss`round'_nm61
drop if fsu==.
drop if _merge==2
drop _merge
ren statename statename`round'
ren districtname districtname`round'
merge m:m statename`round' districtname`round' using district_conversion
drop if fsu==.
drop if _merge==2
replace statenamedist=statename`round' if statenamedist==""
drop _merge
save temp, replace



***Need to get (common?) regioncode for 38th round?
local round 38
*at what level to aggregate? let's say REGION and VILLAGE for now.
use round`round'_edit, clear
merge 1:1 sector subround region subsample segment substratum fsu hhno using round`round'_hh, keepusing(mpce hhsize d* nic* mult)
drop _merge
merge m:m region round using mult`round', keepusing(regioncode)
drop _merge

drop hq* hx*
drop *cloth* *intox* *light*
gen state=floor(region/10)
compress
save temp, replace





local round 66
*at what level to aggregate? let's say REGION and VILLAGE for now.

use round`round'_edit, clear
merge 1:1 sector subround region subsample segment substratum fsu hhno using round`round'_hh, keepusing(mpce hhsize d* nic* mult)
drop hq* hx*
drop *cloth* *intox* *light*
drop _merge

merge m:m region round using mult`round', keepusing(regioncode)
drop _merge


gen state=floor(region/10)

******
gen statecode=state
cap: gen district=.
cap: gen stratum=.
cap: gen districtcode=district
cap: replace districtcode=stratum if round==50
sort sector region fsu
bysort fsu sector: egen minddist=min(districtcode)
bysort fsu sector: egen maxddist=max(districtcode)
replace districtcode=minddist if districtcode==.
replace districtcode=maxddist if districtcode==0 & maxddist~=.
merge m:m statecode districtcode using nss`round'_nm61
drop if fsu==.
drop if _merge==2
drop _merge
ren statename statename`round'
ren districtname districtname`round'
merge m:m statename`round' districtname`round' using district_conversion
drop if fsu==.
drop if _merge==2
replace statenamedist=statename`round' if statenamedist==""
drop _merge


compress
merge m:1 round regioncode sector using pindex_rur_revised


drop if regioncode==.
drop if region==.

gen nfood=0
gen nfoodtot=0


foreach i in grains pulses meat milk oil veg fru sugar bev proc{
replace nfood=nfood+n`i'
replace nfoodtot=nfoodtot+n`i'tot
}




egen vill=group(round sector regioncode fsu)
bysort vil: gen vcounter=_N
tab vcounter


cap: drop state


****NOTE: regions are called states for this analysis
gen state=regioncode


gen statetotxfood=0
gen villtotxfood=0



foreach i in grains veg{
gen statetotx`i'=0
gen villtotx`i'=0
gen share`i'=x`i'/xfood
replace share`i'=0 if share`i'==.

levelsof n`i'tot, local(nmax)
forvalues j=1(1)`nmax'{ 

gen uv`i'_`j'=x`i'_`j'/q`i'_`j'
replace uv`i'_`j'=. if uv`i'_`j'<=0
gen presence`i'_`j'=0
replace presence`i'_`j'=1 if x`i'_`j'>0 & x`i'_`j'<.


gen share`i'_`j'=x`i'_`j'/x`i'
replace share`i'_`j'=0 if share`i'_`j'==.

foreach agg in vill{
bysort `agg': egen `agg'meduv`i'_`j'=median(uv`i'_`j') 
bysort `agg': egen `agg'totx`i'_`j'=total(x`i'_`j'*mult) 
bysort `agg': egen `agg'count`i'_`j'=total(presence`i'_`j'*mult) 
**average of individual shares is not the same as aggregate share
bysort `agg': egen `agg'meanshare`i'_`j'=total(share`i'_`j'*mult) 
replace `agg'meanshare`i'_`j'=`agg'meanshare`i'_`j'/`agg'count`i'_`j'
gen `agg'meanx`i'_`j'=`agg'totx`i'_`j'/`agg'count`i'_`j'
replace `agg'totx`i'=`agg'totx`i'+`agg'totx`i'_`j'
replace `agg'totxfood=`agg'totxfood+`agg'totx`i'_`j'
}


foreach agg in state{
bysort `agg': egen b`agg'meduv`i'_`j'=median(uv`i'_`j') if round==66
bysort `agg': egen b`agg'totx`i'_`j'=total(x`i'_`j'*mult) if round==66
bysort `agg': egen b`agg'count`i'_`j'=total(presence`i'_`j'*mult) if round==66
bysort `agg': egen b`agg'meanshare`i'_`j'=total(share`i'_`j'*mult) if round==66

foreach k in meduv totx count meanshare{
bysort `agg': egen `agg'`k'`i'_`j'=min(b`agg'`k'`i'_`j')
drop b`agg'`k'`i'_`j'
}

replace `agg'meanshare`i'_`j'=`agg'meanshare`i'_`j'/`agg'count`i'_`j'
gen `agg'meanx`i'_`j'=`agg'totx`i'_`j'/`agg'count`i'_`j'
replace `agg'totx`i'=`agg'totx`i'+`agg'totx`i'_`j'
replace `agg'totxfood=`agg'totxfood+`agg'totx`i'_`j'
}

drop x`i'_`j' uv`i'_`j' q`i'_`j'
}
}



**convert expenditures and numhh into shares (normalizing across locations!) meanx is already normalized

****For psi regression
gen sigmagrains=2.16
gen sigmaveg=1.99

bysort vill: egen villcount=total(mult)
bysort state: egen bstatecount=total(mult) if round==66
bysort state: egen statecount=min(bstatecount)

foreach agg in state vill{
foreach i in grains veg{
gen `agg'share`i'=`agg'totx`i'/`agg'totxfood
levelsof n`i'tot, local(nmax)
forvalues j=1(1)`nmax'{ 
gen `agg'share`i'_`j'=`agg'totx`i'_`j'/`agg'totx`i'
replace `agg'count`i'_`j'=`agg'count`i'_`j'/`agg'count
*tie breaker
replace `agg'meduv`i'_`j'=`agg'meduv`i'_`j'+rnormal(0,0.01)

}
drop `agg'totx`i'*
}
}


aorder

foreach z in vill state{
foreach k in share count meanx{
foreach i in grains veg {
levelsof n`i'tot, local(nmax)
forvalues j=1(1)`nmax'{
gen rank`z'`k'`i'_`j'=.
}

forvalues h=1(1)`nmax'{
egen tmax=rowmax(`z'`k'`i'_1-`z'`k'`i'_`nmax')
forvalues j=1(1)`nmax'{
cap: replace rank`z'`k'`i'_`j'=`h' if tmax==`z'`k'`i'_`j' & tmax~=. & `z'`k'`i'_`j'~=0 & `z'`k'`i'_`j'~=.
replace `z'`k'`i'_`j'=. if rank`z'`k'`i'_`j'==`h'
}
drop tmax
}
}
}
}


foreach z in vill state{
foreach k in meduv{
foreach i in grains veg {
levelsof n`i'tot, local(nmax)

forvalues j=1(1)`nmax'{
gen rank`z'`k'`i'_`j'=.
}

forvalues h=1(1)`nmax'{
egen tmin=rowmax(`z'`k'`i'_1-`z'`k'`i'_`nmax')
forvalues j=1(1)`nmax'{
cap: replace rank`z'`k'`i'_`j'=`nmax'+1-`h' if tmin==`z'`k'`i'_`j' & tmin~=. & `z'`k'`i'_`j'~=0 & `z'`k'`i'_`j'~=.
replace `z'`k'`i'_`j'=. if rank`z'`k'`i'_`j'==`nmax'+1-`h'
}
drop tmin
}
}
}
}


***Now generate hh (expenditure weighted and unweighted) measures of this stuff

foreach k in share count meanx meduv{
foreach agg in rankstate rankvill{

foreach i in grains veg{
gen `agg'hh`k'`i'_uw=0

levelsof n`i'tot, local(nmax)
forvalues j=1(1)`nmax'{ 

replace `agg'`k'`i'_`j'=0 if `agg'`k'`i'_`j'==.
replace presence`i'_`j'=0 if presence`i'_`j'==.
replace share`i'_`j'=0 if share`i'_`j'==.
replace share`i'=0 if share`i'==.


replace `agg'hh`k'`i'_uw=`agg'hh`k'`i'_uw + (presence`i'_`j'*`agg'`k'`i'_`j') if `agg'`k'`i'_`j'~=.
}
replace `agg'hh`k'`i'_uw=`agg'hh`k'`i'_uw/n`i'
}
}
}

cap: drop logn*
cap: drop logx*
foreach i in grains veg{
gen logx`i'=log(x`i'/pindex`i')
gen logn`i'=log(n`i')
}



gen loghhsize=log(hhsize)
gen loghhsize2=loghhsize^2


gen urban=0
replace urban=1 if sector==2
gen round66=0
replace round66=1 if round==66
gen urbanround66=0
replace urbanround66=1 if urban==1 & round==66




local agg rankstate


**Table 3: Hierarchical Variety consumption

foreach i in grains veg{
local k share
areg `agg'hh`k'`i'_uw n`i' if round==66 & n`i'>0, absorb(state) vce(cluster state) 
outreg2 using hierarchy`i'.tex, dec(2) keep(n`i' logx`i' urban) replace ctitle(`k' `i')

foreach k in share {
areg `agg'hh`k'`i'_uw logx`i' urban if round==66 & n`i'>0, absorb(state) vce(cluster state)
outreg2 using hierarchy`i'.tex, dec(2) keep(n`i' logx`i' urban) append ctitle(`k' `i')
areg `agg'hh`k'`i'_uw n`i' logx`i' urban if round==66 & n`i'>0, absorb(state) vce(cluster state)
outreg2 using hierarchy`i'.tex, dec(2) keep(n`i' logx`i' urban) append ctitle(`k' `i')
}

foreach k in count meanx meduv{
areg `agg'hh`k'`i'_uw n`i' if round==66 & n`i'>0, absorb(state) vce(cluster state) 
outreg2 using hierarchy`i'.tex, dec(2) keep(n`i' logx`i' urban) append ctitle(`k' `i')
areg `agg'hh`k'`i'_uw logx`i' urban if round==66 & n`i'>0, absorb(state) vce(cluster state)
outreg2 using hierarchy`i'.tex, dec(2) keep(n`i' logx`i' urban) append ctitle(`k' `i')
areg `agg'hh`k'`i'_uw n`i' logx`i' urban if round==66 & n`i'>0, absorb(state) vce(cluster state)
outreg2 using hierarchy`i'.tex, dec(2) keep(n`i' logx`i' urban) append ctitle(`k' `i')
}
}




***Appendix Table A.2

local agg rankvill

foreach i in grains veg{
local k share
areg `agg'hh`k'`i'_uw n`i' if round==66 & n`i'>0, absorb(vill) vce(cluster vill) 
outreg2 using vhierarchy`i'.tex, dec(2) keep(n`i') replace ctitle(`k' `i')
areg `agg'hh`k'`i'_uw n`i' logx`i' if round==66 & n`i'>0, absorb(vill) vce(cluster vill) 
outreg2 using vhierarchy`i'.tex, dec(2) keep(n`i' logx`i') append ctitle(`k' `i')
foreach k in count meanx meduv {
areg `agg'hh`k'`i'_uw n`i' if round==66 & n`i'>0, absorb(vill) vce(cluster vill) 
outreg2 using vhierarchy`i'.tex, dec(2) keep(n`i') append ctitle(`k' `i')
areg `agg'hh`k'`i'_uw n`i' logx`i' if round==66 & n`i'>0, absorb(vill) vce(cluster vill) 
outreg2 using vhierarchy`i'.tex, dec(2) keep(n`i' logx`i') append ctitle(`k' `i')

}
}


log close




***Correlations of intensive and extensive margins (reported in the paper)


foreach group in grains veg{
local geo region
*local geo all
use round66_edit, clear

egen vill=group(sector region fsu)
gen all=1

keep x`group'* `geo' n`group'tot
local nmax=n`group'tot[1]

forvalues j=1(1)`nmax'{
gen s`group'_`j'=x`group'_`j'/x`group'
replace s`group'_`j'=. if s`group'_`j'==0
gen p`group'_`j'=0 if x`group'_`j'==0
replace p`group'_`j'=1 if x`group'_`j'>0
bysort `geo': egen ext`group'_`j'=mean(p`group'_`j')
bysort `geo': egen int`group'_`j'=mean(s`group'_`j')
replace ext`group'_`j'=. if ext`group'_`j'==0
replace int`group'_`j'=. if int`group'_`j'==0
}

duplicates drop `geo', force

expand 2
bysort `geo': gen counter=_n
forvalues j=1(1)`nmax'{
gen var`j'=ext`group'_`j' if counter==1
replace var`j'=int`group'_`j' if counter==2
}

egen z=group(`geo')
egen zmax=max(z)
local zmax=zmax[1]

keep var* 
xpose, clear varname
gen vcounter=_n



expand `zmax'
bysort vcounter: gen rcounter=_n
gen extensive=.
gen intensive=.
forvalues j=1(1)`zmax'{
local k=(`j'*2)-1
replace extensive=v`k' if rcounter==`j'
local k=`j'*2
replace intensive=v`k' if rcounter==`j'
}


twoway (scatter intensive extensive, legend(off) title(`group') xtitle("Share of households consuming variety") ytitle("Mean budget share for consuming hh") ) (lfit intensive extensive)
graph save hierarchy_`group', replace



corr intensive extensive
***correlations of 0.63, 0.52 respectively
**0.79, 0.7 at the aggregate (all India) level

}







clear all


foreach group in grains veg{

use round38_edit, clear
append using round66_edit

merge m:m region round using mult38, keepusing(regioncode)
drop _merge
merge m:m region round using mult66, keepusing(regioncode) update
drop _merge

drop region


local geo regioncode 


egen vill=group(sector region fsu)
gen all=1

keep x`group'* `geo' n`group'tot round
local nmax=n`group'tot[1]

forvalues j=1(1)`nmax'{
gen s`group'_`j'=0 if x`group'_`j'==0 & round==66
replace s`group'_`j'=1 if x`group'_`j'>0 & round==66
gen p`group'_`j'=0 if x`group'_`j'==0 & round==38
replace p`group'_`j'=1 if x`group'_`j'>0 & round==38
bysort `geo': egen ext`group'_`j'=mean(p`group'_`j')
bysort `geo': egen int`group'_`j'=mean(s`group'_`j')
replace ext`group'_`j'=. if ext`group'_`j'==0
replace int`group'_`j'=. if int`group'_`j'==0
}

duplicates drop `geo', force

expand 2
bysort `geo': gen counter=_n
forvalues j=1(1)`nmax'{
gen var`j'=ext`group'_`j' if counter==1
replace var`j'=int`group'_`j' if counter==2
}

egen z=group(`geo')
egen zmax=max(z)
local zmax=zmax[1]

keep var* 
xpose, clear varname
gen vcounter=_n



expand `zmax'
bysort vcounter: gen rcounter=_n
gen extensive=.
gen intensive=.
forvalues j=1(1)`zmax'{
local k=(`j'*2)-1
replace extensive=v`k' if rcounter==`j'
local k=`j'*2
replace intensive=v`k' if rcounter==`j'
}



twoway (scatter intensive extensive, legend(off) title(`group') xtitle("Share hh consuming in 1983") ytitle("Share hh consuming in 2009") ) (lfit intensive extensive, lpattern(solid) lcolor(black) range(0 0.8)) (lfit extensive extensive, lpattern(dash))
graph save `group'_scatter, replace


**correlation of ranking over time [or between rural and urban]
bysort rcounter: egen ext_rank=rank(extensive)
bysort rcounter: egen int_rank=rank(intensive)
corr int_rank ext_rank
corr intensive extensive


}




clear all

foreach group in grains veg{

use round66_edit, clear

merge m:m region round using mult66, keepusing(regioncode)
drop _merge

drop region

local geo regioncode 



keep x`group'* `geo' n`group'tot sector
local nmax=n`group'tot[1]

forvalues j=1(1)`nmax'{
gen s`group'_`j'=0 if x`group'_`j'==0 & sector==2
replace s`group'_`j'=1 if x`group'_`j'>0 & sector==2

gen p`group'_`j'=0 if x`group'_`j'==0 & sector==1
replace p`group'_`j'=1 if x`group'_`j'>0 & sector==1
bysort `geo': egen ext`group'_`j'=mean(p`group'_`j')
bysort `geo': egen int`group'_`j'=mean(s`group'_`j')
replace ext`group'_`j'=. if ext`group'_`j'==0
replace int`group'_`j'=. if int`group'_`j'==0
}

duplicates drop `geo', force

expand 2
bysort `geo': gen counter=_n
forvalues j=1(1)`nmax'{
gen var`j'=ext`group'_`j' if counter==1
replace var`j'=int`group'_`j' if counter==2
}

egen z=group(`geo')
egen zmax=max(z)
local zmax=zmax[1]

keep var* 
xpose, clear varname
gen vcounter=_n



expand `zmax'
bysort vcounter: gen rcounter=_n
gen extensive=.
gen intensive=.
forvalues j=1(1)`zmax'{
local k=(`j'*2)-1
replace extensive=v`k' if rcounter==`j'
local k=`j'*2
replace intensive=v`k' if rcounter==`j'
}



twoway (scatter intensive extensive, legend(off) title(`group') xtitle("Share rural hh consuming in 2009") ytitle("Share urban hh consuming in 2009") ) (lfit intensive extensive, lpattern(solid) lcolor(black) range(0 0.8)) (lfit extensive extensive, lpattern(dash))
graph save `group'_scatter_ru66, replace


**correlation of ranking over time [or between rural and urban]
bysort rcounter: egen ext_rank=rank(extensive)
bysort rcounter: egen int_rank=rank(intensive)
corr int_rank ext_rank
corr intensive extensive


}





***Appendix Figure A.5
graph combine grains_scatter.gph veg_scatter.gph grains_scatter_ru66.gph veg_scatter.gph, ycommon xcommon
graph export scatter_shares_all.png, replace
***Grains correlations: 0.8 (ranks), 0.96 (levels)
***Veg correlations: 0.95 (ranks), 0.97 (levels)



**************Simple (non-parametric) test of simple choke price models***
**show that the change in ext. margin is not (necessarily) well predicted by the change in int. margin
*aggregate quantity and number of hh





clear all
foreach group in grains veg{

use round38_edit, clear
append using round66_edit

merge m:m region round using mult38, keepusing(regioncode)
drop _merge
merge m:m region round using mult66, keepusing(regioncode) update
drop _merge

drop region


local geo regioncode round


keep q`group'* x`group'* `geo' n`group'tot round
local nmax=n`group'tot[1]


gen hh=1
bysort `geo': egen numhh=total(hh)

forvalues j=1(1)`nmax'{
gen presence`group'_`j'=0 if x`group'_`j'==0
replace presence`group'_`j'=1 if x`group'_`j'>0
bysort `geo': egen ext`group'_`j'=mean(presence`group'_`j')

replace q`group'_`j'=0 if q`group'_`j'==.

bysort `geo': egen aggq`group'_`j'=total(q`group'_`j')
replace aggq`group'_`j'=aggq`group'_`j'/numhh

bysort regioncode: egen minext`group'_`j'=min(ext`group'_`j')
sort `geo' q`group'_`j'
by `geo': gen hcounter`group'_`j'=_n
gen hpctile`group'_`j'=hcounter`group'_`j'/numhh
gen cut`group'_`j'=0
replace cut`group'_`j'=1 if hpctile`group'_`j'>1-minext`group'_`j'
bysort `geo': egen aggqcut`group'_`j'=total(q`group'_`j'*cut`group'_`j')
replace aggqcut`group'_`j'=aggqcut`group'_`j'/numhh

replace aggq`group'_`j'=. if aggq`group'_`j'==0
replace aggqcut`group'_`j'=. if aggqcut`group'_`j'==0
replace ext`group'_`j'=. if ext`group'_`j'==0
}
duplicates drop `geo', force

foreach var in aggq aggqcut ext{
forvalues j=1(1)`nmax'{
gen b`var'`group'_`j'=`var'`group'_`j' if round==38
bysort regioncode: egen base`var'`group'_`j'=min(b`var'`group'_`j')
gen d`var'`group'_`j'=ln(`var'`group'_`j')-ln(base`var'`group'_`j') if round==66
drop b*`var'`group'_`j'
}
}
keep if round==66
keep regioncode d*

expand 2
bysort regioncode: gen counter=_n
forvalues j=1(1)`nmax'{
gen var`j'=dext`group'_`j' if counter==1
replace var`j'=daggq`group'_`j' if counter==2
}

egen z=group(regioncode)
egen zmax=max(z)
local zmax=zmax[1]

keep var* 
xpose, clear varname
gen vcounter=_n

expand `zmax'
bysort vcounter: gen rcounter=_n
gen extensive=.
gen intensive=.
forvalues j=1(1)`zmax'{
local k=(`j'*2)-1
replace extensive=v`k' if rcounter==`j'
local k=`j'*2
replace intensive=v`k' if rcounter==`j'
}

reg extensive intensive, robust
twoway (scatter extensive intensive, xtitle("Aggregate quantity") ytitle("Frac. hh with quantity>0") title(`group') xline(0) yline(0))
graph save intext_`group', replace
gen off=0 if extensive~=. & intensive~=.
replace off=1 if extensive>0 & intensive<0 & extensive~=. & intensive~=.
replace off=1 if extensive<0 & intensive>0 & extensive~=. & intensive~=.
summ off
}

graph combine intext_grains.gph intext_veg.gph

***Appendix Figure A.6
graph export intextcorr.pdf, replace
**For grains, some 26% of observations display negative correlation of agg quantity and share of hh consuming [over time]
**For veg it is: 22% [over time]
***Using the percentile cutoff criteria, it is 41% for both that are in the off main-diagonal...
**Using all India (instead of regions)  56% and 55% are off main-diagonal





clear all
foreach group in grains veg{

use round66_edit, clear

merge m:m region round using mult66, keepusing(regioncode) update
drop _merge

drop region

local geo regioncode sector


keep q`group'* x`group'* `geo' n`group'tot sector
local nmax=n`group'tot[1]




gen hh=1
bysort `geo': egen numhh=total(hh)

forvalues j=1(1)`nmax'{
gen presence`group'_`j'=0 if x`group'_`j'==0
replace presence`group'_`j'=1 if x`group'_`j'>0
bysort `geo': egen ext`group'_`j'=mean(presence`group'_`j')

replace q`group'_`j'=0 if q`group'_`j'==.

bysort `geo': egen aggq`group'_`j'=total(q`group'_`j')
replace aggq`group'_`j'=aggq`group'_`j'/numhh

bysort regioncode: egen minext`group'_`j'=min(ext`group'_`j')
sort `geo' q`group'_`j'
by `geo': gen hcounter`group'_`j'=_n
gen hpctile`group'_`j'=hcounter`group'_`j'/numhh
gen cut`group'_`j'=0
replace cut`group'_`j'=1 if hpctile`group'_`j'>1-minext`group'_`j'

bysort `geo': egen aggqcut`group'_`j'=total(q`group'_`j'*cut`group'_`j')
replace aggqcut`group'_`j'=aggqcut`group'_`j'/numhh

replace aggq`group'_`j'=. if aggq`group'_`j'==0
replace aggqcut`group'_`j'=. if aggqcut`group'_`j'==0
replace ext`group'_`j'=. if ext`group'_`j'==0
}
duplicates drop `geo', force

foreach var in aggq aggqcut ext{
forvalues j=1(1)`nmax'{
gen b`var'`group'_`j'=`var'`group'_`j' if sector==1
bysort regioncode: egen base`var'`group'_`j'=min(b`var'`group'_`j')
gen d`var'`group'_`j'=ln(`var'`group'_`j')-ln(base`var'`group'_`j') if sector==2
drop b*`var'`group'_`j'
}
}
keep if sector==2
keep regioncode d*

expand 2
bysort regioncode: gen counter=_n
forvalues j=1(1)`nmax'{
gen var`j'=dext`group'_`j' if counter==1
replace var`j'=daggqcut`group'_`j' if counter==2
}

egen z=group(regioncode)
egen zmax=max(z)
local zmax=zmax[1]

keep var* 
xpose, clear varname
gen vcounter=_n

expand `zmax'
bysort vcounter: gen rcounter=_n
gen extensive=.
gen intensive=.
forvalues j=1(1)`zmax'{
local k=(`j'*2)-1
replace extensive=v`k' if rcounter==`j'
local k=`j'*2
replace intensive=v`k' if rcounter==`j'
}

reg extensive intensive, robust
twoway (scatter extensive intensive, xtitle("Total quantity") ytitle("Num hh")) (lfitci extensive intensive)
gen off=0 if extensive~=. & intensive~=.
replace off=1 if extensive>0 & intensive<0 & extensive~=. & intensive~=.
replace off=1 if extensive<0 & intensive>0 & extensive~=. & intensive~=.
summ off
}

**Between rural and urban?
*14% grains and 18% vegetables
**Using the percentile cutoff for quantity it is 19% and 23%
**For all India (pooling regions) it's 11% and 21%




clear all
foreach group in grains veg{
use round38_edit, clear
append using round66_edit

merge m:m region round using mult38, keepusing(regioncode)
drop _merge
merge m:m region round using mult66, keepusing(regioncode) update
drop _merge

drop region

local geo regioncode round


keep q`group'* x`group'* `geo' n`group'tot round
local nmax=n`group'tot[1]

forvalues j=1(1)`nmax'{
replace q`group'_`j'=0 if qgrains_`j'==.
gen q`group'_38=q`group'_`j' if round==38
gen q`group'_66=q`group'_`j' if round==66
qqplot qgrains_38 qgrains_66
save var`group'`j', replace
drop q`group'_38 q`group'_66
}

}







**************Table 4: Shopping time analysis***********



use "`rootdir'TUS\level2", clear

gen distribution=0
gen retail=0
gen food=0
gen people=1

foreach j in mainindustry secindustry{
replace distribution=1 if `j'>="600" & `j'<"800"
replace retail=1 if `j'>="600" & mainindustry<"700"
replace food=1 if `j'=="600"
replace food=1 if `j'=="601"
replace food=1 if `j'=="602"
replace food=1 if `j'=="604"
replace food=1 if (`j'>="605" & `j'<"609")
replace food=1 if `j'=="641"
replace food=1 if `j'>="650" & `j'<"660"
replace food=1 if `j'=="688"
replace food=1 if `j'=="690"
}

summ distribution retail food

foreach j in people distribution retail food{
bysort state district sector: egen `j'wrk=total(`j')
bysort state district sector: egen `j'share=mean(`j')

bysort state district: egen d`j'wrk=total(`j')
bysort state district: egen d`j'share=mean(`j')

}

duplicates drop state district sector, force
keep state district sector *wrk *share
save retail, replace



use "`rootdir'TUS\level4", clear
compress

merge m:1 state district sector using retail
drop _merge

gen shop=0
replace shop=acttime if actcode==441
gen cook=0
replace cook=acttime if actcode==411
gen travel=0
replace travel=acttime if actcode==481
gen eat=0
replace eat=acttime if actcode==921
gen alltravel=0
replace alltravel=acttime if (actcode==119 | actcode==129 | actcode==139 | actcode==149 | actcode==159 | actcode==169 | actcode==219 | actcode==229 | actcode==319 | actcode==329 | actcode==571 | actcode==572 | actcode==691 | actcode==791 | actcode==891)
*leave out 481

gen leisure=0
replace leisure=acttime if actcode>=811 & actcode<891
replace leisure=acttime if actcode>=911 & actcode<=981


gen process=0
replace process=acttime if actcode==152
replace process=acttime if actcode==153
replace process=acttime if actcode==154
replace process=acttime if actcode==155

gen gather=0
replace gather=acttime if actcode==116
replace gather=acttime if actcode==141

foreach k in shop cook travel eat alltravel leisure process gather{
replace `k'=0 if `k'==.
bysort hhid persno daytype: egen tot`k'=total(`k')
replace tot`k'=0 if tot`k'==.
drop `k'
}

duplicates drop hhid persno daytype, force


bysort hhid persno: egen totdays=total(days)
summ totdays
gen normal=0
replace normal=1 if daytype==1
bysort hhid persno: egen totdays_normal=total(days*normal)

foreach k in shop cook travel eat alltravel leisure process gather{
bysort hhid persno: egen `k'time=total(days*tot`k')
replace `k'time=`k'time/totdays
bysort hhid persno: egen `k'time_normal=total(days*normal*tot`k')
replace `k'time_normal=`k'time_normal/totdays_normal
}
duplicates drop hhid persno, force

merge 1:1 sector state serialno sstratum hhno persno using "`rootdir'TUS\level2"
drop _merge
drop update
merge m:1 sector state serialno sstratum hhno using "`rootdir'TUS\level1"
drop _merge


gen servant=0
replace servant=1 if relhh==9
gen wife=0
replace wife=1 if relhh==2 & sex==2
gen woman=0
replace woman=1 if sex==2


bysort hhid: egen totwife=total(wife)
bysort hhid: egen totwoman=total(woman)
bysort hhid: egen totser=max(servant)

**construct a few household demographics
gen af=0
replace af=1 if age>17 & sex==2
gen am=0
replace am=1 if age>17 & sex==1
bysort hhid: egen rm=total(am/hhsize)
bysort hhid: egen rf=total(af/hhsize)



gen combtime=shoptime+traveltime

foreach k in shop travel cook eat alltravel comb leisure process gather{
bysort hhid: egen hh`k'=total(`k'time)
bysort hhid: egen hh`k'_noser=total(`k'time*servant)
bysort hhid: egen hh`k'_wife=total(`k'time*wife)
bysort hhid: egen hh`k'_woman=total(`k'time*woman)

gen ext_`k'=0
replace ext_`k'=1 if hh`k'>0 & hh`k'<.
gen ext_`k'_noser=0
replace ext_`k'_noser=1 if hh`k'_noser>0 & hh`k'_noser<.
gen ext_`k'_wife=0
replace ext_`k'_wife=1 if hh`k'_wife>0 & hh`k'_wife<.
gen ext_`k'_woman=0
replace ext_`k'_woman=1 if hh`k'_woman>0 & hh`k'_woman<.


gen ln`k'=log(hh`k')
gen ln`k'_noser=log(hh`k'_noser)
gen ln`k'_wife=log(hh`k'_wife)
gen ln`k'_woman=log(hh`k'_woman)

gen ln1`k'=log(1+hh`k')
gen ln1`k'_noser=log(1+hh`k'_noser)
gen ln1`k'_wife=log(1+hh`k'_wife)
gen ln1`k'_woman=log(1+hh`k'_woman)
}

duplicates drop hhid, force

bysort sector: summ hh* ext*
summ hh* ext*





***merging in district variety data..
merge m:1 state district sector using "`rootdir'var_districts_tus"
drop _merge
merge m:m state district using "`rootdir'var_districts_tus", update
drop _merge



foreach j in dfood dpeople dretail ddistribution{
gen `j'sqkm=`j'wrk/areasqkm
}




egen zz=group(state district)
egen zzz=group(state district sector)


*distclass 1-3 big to small towns and villages


gen urban=0
replace urban=1 if sector==2
gen lnxpc=log(mpce/pindex)
gen lnexp=log((mpce*hhsize)/pindex)

gen totexp=mpce*hhsize
bysort state district sector: egen meanexp=mean(totexp/pindex)
bysort state district: egen dmeanexp=mean(totexp/pindex)

gen lnmeanexp=ln(meanexp)
gen lndmeanexp=ln(dmeanexp)

gen lnhh=log(hhsize)
gen lnhh2=lnhh^2



gen bigtown=0
replace bigtown=1 if distclass==1 & sector==2

gen medtown=0
replace medtown=1 if distclass==2 & sector==2

gen smalltown=0
replace smalltown=1 if distclass==3 & sector==2

gen bigvill=0
replace bigvill=1 if distclass==1 & sector==1

gen medvill=0
replace medvill=1 if distclass==2 & sector==1

gen bdistrict=0
replace bdistrict=1 if stratum==3 | stratum==4



egen block=group(sector state district town serialno hamlet sstratum)


**occupation? basic demographics
destring nic, replace force
gen nic1=floor(nic/100)

forvalues j=0(1)8{
gen nic_`j'=0
replace nic_`j'=1 if nic1==`j'
}





cap: gen farmer=0
cap: replace farmer=1 if sector==1 & htype==5


cap: gen othtown=0
cap: replace othtown=1 if smalltown==1 | medtown==1

cap: gen hhprocesscook=hhcook+hhprocess
cap: replace meanxfood=ln(meanxfood)


local z comb
local y lnexp
local controls lnhh rm rf i.hgroup i.religion farmer i.struct



***********Time-use Table 4
areg hh`z' `y' `controls', absorb(block) vce(robust)
outreg2 using timeuse_rev.tex, keep(lnexp) replace ctitle(shop)

reg hh`z' `y' othtown bigtown `controls', vce(cluster zz)
outreg2 using timeuse_rev.tex, keep(lnexp othtown bigtown) append ctitle(shop)

cap: gen lndensity=ln(densitysqkm)
local k dfoodshare lndensity lndmeanexp
reg hh`z' `y' `controls' `k', vce(cluster zz)
outreg2 using timeuse_rev.tex, keep(lnexp dfoodshare lndensity lndmeanexp) append ctitle(shop)

reg hh`z' `y' `controls' meann meanxfood, vce(cluster zz)
outreg2 using timeuse_rev.tex, keep(lnexp meann meanxfood) append ctitle(shop)




***********Appendix: Table A.3*******



foreach j in ext_comb{
reg `j' `y' `controls' dfoodshare lndensity lndmeanexp, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp dfoodshare lndensity lndmeanexp) replace ctitle(`j')
reg `j' `y' `controls' meann meanxfood, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp meann meanxfood) append ctitle(`j')
}

local j hhcomb
reg `j' `y' `controls' dfoodshare lndensity lndmeanexp if totser==0, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp dfoodshare lndensity lndmeanexp) append ctitle(`j')
reg `j' `y' `controls' meann meanxfood if totser==0, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp meann meanxfood) append ctitle(`j')

local j hhcomb_wife
reg `j' `y' `controls' dfoodshare lndensity lndmeanexp if totwife>0, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp dfoodshare lndensity lndmeanexp) append ctitle(`j')
reg `j' `y' `controls' meann meanxfood if totwife>0, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp meann meanxfood) append ctitle(`j')

foreach j in hhalltravel hhcook hhleisure {
reg `j' `y' `controls' dfoodshare lndensity lndmeanexp, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp dfoodshare lndensity lndmeanexp) append ctitle(`j')
reg `j' `y' `controls'  meann meanxfood, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp meann meanxfood) append ctitle(`j')
}


local j hhprocess
reg `j' `y' `controls' dfoodshare lndensity lndmeanexp, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp dfoodshare lndensity lndmeanexp) append ctitle(`j')
reg `j' `y' `controls'  meann meanxfood, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp meann meanxfood) append ctitle(`j')

local j hhgather
reg `j' `y' `controls' dfoodshare lndensity lndmeanexp, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp dfoodshare lndensity lndmeanexp) append ctitle(`j')
reg `j' `y' `controls'  meann meanxfood, vce(cluster zz)
outreg2 using timeuse_app.tex, keep(lnexp meann meanxfood) append ctitle(`j')


*Put at the bottom of the table
summ hhcomb hhalltravel hhcook hhleisure lndensity dfoodshare lndmeanexp meann meanxfood ext_comb hhprocess hhgather 
*    Variable |       Obs        Mean    Std. Dev.       Min        Max
*-------------+--------------------------------------------------------
*      hhcomb |     18592    28.63364    44.40216          0        900
* hhalltravel |     18592    126.2928     122.727          0       1570
*      hhcook |     18592    222.4606    140.9523          0   3111.428
*   hhleisure |     18592    3195.781    1626.756          0   16495.71
*   lndensity |     18755    6.225834    1.293835   3.828641   10.19999
*-------------+--------------------------------------------------------
*  dfoodshare |     18591    .0238553    .0084436   .0092264   .0436508
*  lndmeanexp |     18592    7.705529    .1937267   7.294381   8.098853
*       meann |     18755    29.50461    5.813158   15.42025    45.3689
*   meanxfood |     18755    7.281624    .2924136   6.296174   7.949917
*    ext_comb |     18592     .541846    .4982592          0          1
*-------------+--------------------------------------------------------
*   hhprocess |     18592    7.319746     52.1001          0       1700
*    hhgather |     18592     13.6273    42.10286          0   1324.286

summ hhcomb if totser==0
*    Variable |       Obs        Mean    Std. Dev.       Min        Max
*-------------+--------------------------------------------------------
*      hhcomb |     18529     28.6155    44.37348          0        900

summ hhcomb_wife if totwife>0

*    Variable |       Obs        Mean    Std. Dev.       Min        Max
*-------------+--------------------------------------------------------
* hhcomb_wife |     15147    10.52491    24.63604          0        420


tab totser
*     totser |      Freq.     Percent        Cum.
*------------+-----------------------------------
*          0 |     18,529       99.66       99.66
*          1 |         63        0.34      100.00
*------------+-----------------------------------
*      Total |     18,592      100.00















